package com.haina.shop.service.impl;

import com.github.pagehelper.Page;
import com.haina.shop.domain.Prod;
import com.haina.shop.domain.ShopDetail;
import com.haina.shop.mapper.ProdMapper;
import com.haina.shop.mapper.ShopDetailMapper;
import com.haina.shop.service.DailyCrazyDealsService;
import com.haina.shop.vo.PageVo;
import com.haina.shop.vo.ProductVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DailyCrazyDealsServiceImpl implements DailyCrazyDealsService {

    @Resource
    private ProdMapper prodMapper;
    @Resource
    private ShopDetailMapper shopDetailMapper;

    @Override
    public PageVo pageSelect(PageVo pageVo){
        List<Prod> prods = prodMapper.selectByStatus(pageVo);
        long l = prodMapper.selectCount();
        pageVo.setTotal(l);
        pageVo.setPages(pageVo.getCurrent());

        ArrayList<Long> shipIdList = new ArrayList<>();
        for (Prod prod:prods){
            shipIdList.add(prod.getShopId());
        }
        List<ShopDetail> list = shopDetailMapper.selectShopName(shipIdList);
        Map<Long,String> map=new HashMap<>();
        for (ShopDetail shopDetail :list){
            map.put(shopDetail.getShopId(),shopDetail.getShopName());
        }

        List<ProductVO> li=new ArrayList<>();
        for (Prod prod:prods){
            ProductVO productVO=new ProductVO(prod.getBrief(),prod.getPic(),
                    prod.getPrice(),prod.getProdId(),map.get(prod.getShopId()));
            String prodName=prod.getProdName();
            if (prodName.length()>15){
                prodName=prodName.substring(0,15)+"...";
            }
            productVO.setProdName(prodName);
            li.add(productVO);
        }
        pageVo.setRecords(li);
        return pageVo;
    }
}
